package com.vinhvt.effiport.application.sample;

import java.util.ArrayList;
import java.util.List;

import com.vinhvt.effiport.util.Helper;

public class Function {
	private List<Variable> vars = new ArrayList<Variable>();
	private int size = 0;
		
	public double calcValue() {
		double result = 0.0;
				
		double var1 = this.getVariable(0).getValue();
		double var2 = this.getVariable(1).getValue();
		
		double x1 = 0;
		double x2 = 0;				
			x1 = Helper.round((var1 * 100) / (var1 + var2));
			x2 = Helper.round((var2 * 100) / (var1 + var2));		
						
		
		result  = 20000 - 440 * x1 - 300 * x2;
		result += 20 * x1 * x1 + 12 * x2 * x2;
		result += x1 * x2;
		
		
		return result;
	}
	
	public void addVariable(Variable var, double value) {
		var.setValue(value);
		this.vars.add(var);
		this.size++;
	}
	
	public void addVariable(int position, Variable var, double value) {
		var.setValue(value);
		this.vars.add(position, var);
		this.size++;
	}
	
	public void removeVariable(int index) {
		this.vars.remove(index);
		this.size--;
	}
	
	public Variable getVariable(int index) {
		return this.vars.get(index);
	}
	
	public void clear() {
		this.vars.clear();
		this.size = 0;
	}
	
	public int getSize() {
		return this.size;
	}
	
	@Override
	public String toString() {
		String result = "";
		
		double var1 = this.getVariable(0).getValue();
		double var2 = this.getVariable(1).getValue();
		
		double x1 = Helper.round((var1 * 100) / (var1 + var2));
		double x2 = Helper.round((var2 * 100) / (var1 + var2));
				
		result += "\nX1 = " + x1;
		result += "\nY2 = " + x2;
		result += "\nValue = " + this.calcValue();
		
		return result;
		
	}
}
